package com.innovate.nms.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Ping {

	private static final String CMD 	= "cmd.exe /c ping ";
	private static final String CMD_WIN = "cmd.exe /c start ping ";
	
	public static interface Printer {
		void doPrint(String text);
	}
	
	public static boolean ping(String ip, long timeout, Printer p) {
		
		if(ip == null || ip.isEmpty())
			return false;
		
		Process process = null;
		
		try {
			
			process = Runtime.getRuntime().exec(CMD + ip);
			
			BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK")); 
			
			long start = System.currentTimeMillis();
            
            while(true)       
            { 
				String s = br.readLine();
				
				if(s==null) 
					break; 
				
				if(p != null) {
					p.doPrint(s);
				}
				
				if(s.contains("TTL=")) 
					return true;
				
				long now = System.currentTimeMillis();
				if(now - start > timeout)
					break;

            } 
			
			//process.waitFor();	
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		} finally {
			
			if(process != null) {
				process.destroy();
				process = null;
			}
			
		}
		
		return false;
		
	}
	
	public static boolean ping(String ip, long timeout) {
		
		return ping(ip, timeout, null);
		
	}
	
	
	public static void systemPing(String ip) {
		try {
			Runtime.getRuntime().exec(CMD_WIN + ip);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/*
	
	public static void pingWithView(String ip) {
		
		PingWorker worker = new PingWorker(ip);
		PingDialog dialog = new PingDialog(ip);
		
		worker.addPropertyChangeListener(dialog);
		worker.execute();
		dialog.setVisible(true);
		worker.removePropertyChangeListener(dialog);
		
	}
	
	static class PingWorker extends SwingWorker<Object, Object> {
		
		String address;

		public PingWorker(String address) {
			super();
			this.address = address;
		}

		@Override
		protected Object doInBackground() throws Exception {
			
			
			Process process = null;
			
			try {
				
				process = Runtime.getRuntime().exec(CMD + address);
				
				BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); 
	            
				int count = 0;
				
	            while(true)       
	            { 
					String s = br.readLine();
					
					if(s==null) 
						break; 
					
					this.firePropertyChange("msg", null, s);
					
					count++;
					
					if(count > 10) 
						break;

	            }
				
			} catch (Exception e) {
				
				e.printStackTrace();
				
			} finally {
				
				if(process != null) {
					process.destroy();
					process = null;
				}
				
			}
			
			return null;
		}
		
	}
	
	static class PingDialog extends JDialog implements PropertyChangeListener {


		private static final long serialVersionUID = 552650550626049159L;
		
		JTextArea textArea;
		
		public PingDialog(String ip) {
			
			setModal(true);
			setResizable(false);
			setTitle("ping " + ip);
			setBounds(100, 100, 639, 443);
			
			JScrollPane scrollPane = new JScrollPane();
			getContentPane().add(scrollPane, BorderLayout.CENTER);
			
			textArea = new JTextArea();
			textArea.setFont(new Font("Monospaced", Font.BOLD, 16));
			textArea.setBackground(Color.BLACK);
			textArea.setForeground(Color.WHITE);
			scrollPane.setViewportView(textArea);

		}


		@Override
		public void propertyChange(PropertyChangeEvent evt) {
			
			if(evt.getPropertyName().equals("msg")) {
				textArea.append(evt.getNewValue().toString());
			}
			
		}
		
		

	}
	*/
}
